﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;

namespace ChinaDynastyTours.Views
{
    public partial class CommissionScheduleVerifyForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtCommissionData;

        public CommissionScheduleVerifyForm()
        {
            InitializeComponent();
        }

        private void ShopCommissionForm_Load(object sender, EventArgs e)
        {
            #region 初始化查询条件，并首次查询

            //默认为上月1日起，下下月1日止
            dateStart.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Month > 1 ? DateTime.Now.Year : DateTime.Now.Year - 1, DateTime.Now.Month > 1 ? DateTime.Now.Month - 1 : 12));
            dateEnd.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Year + DateTime.Now.Month / 11, (DateTime.Now.Month + 1) % 12 + 1));

            //初始化部门列表
            departmentLookupEdit.DataSource = BaseInfoCtrl.GetDepartmentData(0);

            //初始化组团人列表
            //ownerALookUpEdit.DataSource = UserInfoCtrl.GetUserInfoData();

            btnRefresh.PerformClick();
        }
            #endregion
        //查询团数据
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DateTime dateStart = DateTime.Parse(this.dateStart.EditValue.ToString());
            DateTime dateEnd = DateTime.Parse(this.dateEnd.EditValue.ToString());

            string ownerA = tbOwnerA.EditValue == null ? "" : tbOwnerA.EditValue.ToString();
            teamGridControl.DataSource = CommissionCtrl.GetTeamGroupData(dateStart, dateEnd, ownerA);//根据选择的部门过滤
        }

        private void commissionGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                 //状态为‘录入’时允许修改
                if (commissionGridView.GetRowCellValue(e.RowHandle, "VerifyState").ToString() == ShopCommissionEnum.录入.ToString())
                {
                    CommissionScheduleVerifyModifyForm mform = new CommissionScheduleVerifyModifyForm(_dtCommissionData, e.RowHandle);
                    mform._TourGroupID = teamGridView.GetRowCellValue(teamGridView.FocusedRowHandle, "TourgroupID").ToString();
                    mform._TourGroupState = teamGridView.GetRowCellValue(teamGridView.FocusedRowHandle, "TourgroupState").ToString();
                    mform.Owner = this;
                    mform.ShowDialog();
                    //刷新“返佣信息”列表
                    teamGridView_FocusedRowChanged(teamGridView, new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs(0, teamGridView.FocusedRowHandle));
                }
            }
        }

        #region 修改、提交按钮事件处理
    
        private void btnModify_Click(object sender, EventArgs e)
        {
            if (_dtCommissionData == null || _dtCommissionData.Rows.Count == 0) return;

            commissionGridView_RowClick(sender, new DevExpress.XtraGrid.Views.Grid.RowClickEventArgs(new DevExpress.Utils.DXMouseEventArgs(MouseButtons.Left, 2, 0, 0, 0), commissionGridView.FocusedRowHandle));
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (_dtCommissionData == null || _dtCommissionData.Rows.Count == 0) return;

            if (MessageBox.Show("通过审核？", "审核确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string tourgroupId = teamGridView.GetRowCellValue(teamGridView.FocusedRowHandle, "TourgroupID").ToString();
                if (CommissionCtrl.UpdateCommissionState(tourgroupId,ShopCommissionEnum.审核) >= 1)
                {
                    MessageBox.Show("审核完成！！");
                    teamGridView_FocusedRowChanged(teamGridView, new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs(teamGridView.FocusedRowHandle, teamGridView.FocusedRowHandle));
                }
                else
                {
                    MessageBox.Show("系统故障，审核失败！！");
                }
            }
        }

        #endregion

        private void teamGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle < 0) return;

            string tourgroupId = teamGridView.GetRowCellValue(teamGridView.FocusedRowHandle, "TourgroupID").ToString();
            _dtCommissionData = CommissionCtrl.GetCommissionData(tourgroupId);
            commissionGridControl.DataSource = _dtCommissionData;

            //btnModify.Enabled = _dtCommissionData.Rows.Count > 0;
            //btnSubmit.Enabled = _dtCommissionData.Rows.Count > 0;
        }


    }
}